﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using BLL;
using Model;
using System.Data;

namespace Presentation
{
    public partial class CourseList : System.Web.UI.Page
    {
        //declare componnents in web page
        private ContentPlaceHolder mpContentPlaceHolder;
        private TextBox CourseName;
        private RadioButton Open;
        private RadioButton Close;
        private int CourseStatus = -1;
        private DropDownList TeacherName;
        private DropDownList CourseLevel;
        private GridView CourseListView;
        private static Model.Condition cdt;


        protected void Page_Load(object sender, EventArgs e)
        {
            //get componnents in master page 
            mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
            CourseName = (TextBox)mpContentPlaceHolder.FindControl("CourseName");
            TeacherName = (DropDownList)mpContentPlaceHolder.FindControl("TeacherName");
            CourseLevel = (DropDownList)mpContentPlaceHolder.FindControl("CourseLevel");
            Open = (RadioButton)mpContentPlaceHolder.FindControl("StatusOpen");
            Close = (RadioButton)mpContentPlaceHolder.FindControl("StatusClose");
            CourseListView = (GridView)mpContentPlaceHolder.FindControl("CourseListView");


            if (!Page.IsPostBack)
            {
                //bind teacher name in dropdownlist
                //   bindTeacherName();
                bindDropTeacherName();

                //bind Course Level in dropdownlist
                //   bindLevel();
                bindDropLevelName();

                //bind course list in gridview
                //  bindCourseList();

                //set default column to sort
                ViewState["SortOrder"] = "CourseId";
                ViewState["OrderDire"] = "ASC";
            }
        }

        private void bindDropLevelName()
        {
            BLL.LevelList l = new BLL.LevelList();

            this.CourseLevel.DataSource = l.getList();
            this.CourseLevel.DataTextField = "LevelName";
            this.CourseLevel.DataValueField = "LevelId";
            this.CourseLevel.DataBind();
            this.CourseLevel.Items.Insert(0, "--Choose Level--");

            ListBox lb = new ListBox();

        }

        private void bindDropTeacherName()
        {
            BLL.TeacherList t = new BLL.TeacherList();

            this.TeacherName.DataSource = t.getList();
            this.TeacherName.DataTextField = "TeacherName";
            this.TeacherName.DataValueField = "TeacherId";
            this.TeacherName.DataBind();
            this.TeacherName.Items.Insert(0, "--Choose Teacher--");
            this.TeacherName.SelectedIndex = 0;
        }



        protected void SearchBtnClick(object sender, EventArgs e)
        {
            //bind input data into BLL.Condition
            if (Open.Checked)
            {
                CourseStatus = 1;
            }
            else if (Close.Checked)
            {
                CourseStatus = 0;
            }
            else
            {
                CourseStatus = -1;
            }
            string TeacherName = this.TeacherName.SelectedItem.Text.Trim();
            string LevelName = this.CourseLevel.SelectedItem.Text.Trim();
            string CourseNameInput = this.CourseName.Text.Trim();

            cdt = new Model.Condition(CourseNameInput, CourseStatus, TeacherName, LevelName);
            bindReport();


        }

        private void bindReport()
        {
            //pass condition
            BLL.CourseSearch cs = new BLL.CourseSearch();
                        
            DataView view = cs.generateReport(cdt).Tables["searchList"].DefaultView;
            //sort by column name
            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            view.Sort=sort;
           
            CourseListView.DataSource = view; ;
            CourseListView.DataBind();

            
            //find no record ?
            if (cs.generateReport(cdt).Tables["searchList"].Rows.Count == 0)
            {
                errMsg.Visible = true;
                
            }
            else
            {
                errMsg.Visible = false;
            }
        }


        protected void CourseListView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //before paging
            CourseListView.PageIndex = e.NewPageIndex;
            bindReport();
        }

        protected void CourseList_Sorting(object sender,GridViewSortEventArgs e)
        {
            String sortExpression = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == sortExpression)
            {
                string test;
                if (ViewState["OrderDire"].ToString() == "Desc")
                {
                    ViewState["OrderDire"] = "ASC";
                    test = ViewState["OrderDire"].ToString();
                }
                else
                {
                    ViewState["OrderDire"] = "Desc";
                    test = ViewState["OrderDire"].ToString();
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            bindReport();

        }



        protected void CourseListView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }


}
